package com.xzcedu.jdbc.resultest_;

import java.io.FileInputStream;
import java.io.Reader;
import java.sql.*;
import java.util.Properties;

/**
 * @author 徐州城
 * @version 1.0
 * 演示select 语句返回 ResultSet ，并取出结果
 */
public class ResultSet_ {
    @SuppressWarnings("all")
    public static void main(String[] args)throws Exception {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\com\\xzcedu\\jdbc\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1. 注册驱动
        Class.forName(driver);//建议写上

        //2. 得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //3. 得到Statement
        Statement statement = connection.createStatement();

        //4. 组织Sql
        String sql = "select id,name,sex,borndate from actor";
        //执行给定的SQL语句，该语句返回单个 ResultSet对象
        /*
        +----+-----------+-----+---------------------+
        | id | name      | sex | borndate            |
        +----+-----------+-----+---------------------+
        |  2 | 刘德华    | 男  | 1970-12-12 00:00:00  |
        |  3 | jack      | 男  | 1990-12-12 00:00:00 |
        +----+-----------+-----+---------------------+-------+
         */
        /*
            源码：

         */
        ResultSet resultSet = statement.executeQuery(sql);

        //5. 使用while取出数据
        while (resultSet.next()) {//让光标向 下/后 移动，如果没有数据了，返回false
            int id = resultSet.getInt(1);//获取该行第1列数据
            String name = resultSet.getString(2);//获取该行第2列数据
            String sex = resultSet.getString(3);//获取该行第3列数据
            Date date = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
        }

        //6. 关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}